﻿using Helper.Log;
using System;
using System.Configuration;
using System.Windows.Forms;
using CommunServer.Model;
using Helper.ConverttHelper;

namespace CommunServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            txtPort.Text = ConfigurationManager.ConnectionStrings["ServerPort"].ToString();
            btnStart_Click(sender, e);
        }
        /// <summary>
        /// 通信服务
        /// </summary>
        private CommunServer _communServer;
        private void btnStart_Click(object sender, EventArgs e)
        {

            try
            {
                int port;
                if (!int.TryParse(txtPort.Text, out port))
                {
                    MessageBox.Show(@"输入值不是合法的数字", @"错误");
                    return;
                }

                //通信服务
                _communServer = new CommunServer(port);

                _communServer.LogEvent += _communServer_LogEvent;

                btnStart.Enabled = false;
                btnStop.Enabled = true;
            }
            catch (Exception ex)
            {
                ILog log = LogFactory.GetLogger("Main");
                log.Debug(ex);

            }
        }
        private void ShowLog(string text)
        {
            if (ckbShow.Checked)
            {
                if (txtInfo.InvokeRequired)
                {
                    //用于Socket线程向主线程记日志
                    ShowLogHandle d = ShowLog;
                    Invoke(d, text);
                }
                else
                {
                    //主线程直接记日志 
                    txtInfo.Text += text + "\r\n";
                }
            }
            ILog log = LogFactory.GetLogger("通信日志");
            log.Info(text);

        }
        /// <summary>
        /// 显示消息
        /// </summary>
        /// <param name="message"></param>
        private void _communServer_LogEvent(string message)
        {
            ShowLog(message);
        }
        /// <summary>
        /// 停止监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            // _communServer.Stop();
            btnStop.Enabled = false;
            btnStart.Enabled = true;
        }
        /// <summary>
        /// 清除消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtInfo.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  StructHelper.Test();
            LoginReq d = new LoginReq();
            d.Base.CommType = '1';
            d.Base.MesType = '2';
            d.UserName = "ddfs";
            d.Pwd = "123123";
            d.SenData = DateTime.Now.ToString("yyyMMdd");
            d.SenTime = DateTime.Now.ToString("HHmmss");
            byte[] data = StructHelper.StructToBytes(d);
            LoginReq s =  StructHelper.BytesToStuct<LoginReq>(data);
            string cd = "";

        }
    }
}
